//
//  JGMainViewController.m
//  JGADShowFromeNet
//
//  Created by mac on 15/9/8.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGMainViewController.h"
#import "AriesScrollView.h"
#import "AdImgeView.h"
#import "UIView+Category.h"

/** 广告的高度 */
static CGFloat const ADImageView_MaxHeight = 180.0f;

@interface JGMainViewController ()

@end

@implementation JGMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    //初始化广告和数据源
    [self setupADViewDataSource];
}

/**
 * 初始化广告和数据源
 */
-(void)setupADViewDataSource
{
    //广告图的URL
    NSArray *ads = @[
                     @"http://down1.sucaitianxia.com/psd02/psd158/psds28266.jpg",
                     @"http://down1.sucaitianxia.com/psd02/psd158/psds27988.jpg",
                     @"http://www.oschina.net/uploads/space/2011/0428/131242_ULDv_1705.png"
                     ];
    
    //点击广告图的跳转URL
    NSArray *tapUrls = @[
                         @"http://image.baidu.com/",
                         @"http://image.baidu.com/",
                         @"http://image.baidu.com/"
                         ];
    
    
    NSMutableArray *imageViews = [@[] mutableCopy];
    for (int i = 0 ; i < ads.count; i ++)
    {
        NSString *url = ads[i];
        NSString *tapUrl = tapUrls[i];
        if (url) {
            AdImgeView *imageview = [[AdImgeView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, ADImageView_MaxHeight)];
            imageview.imgUrl = url;
            [imageViews addObject:imageview];
            imageview.url = tapUrl;
            if (tapUrl) {
                [imageview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToAdUrl:)]];
            }
            
        }
    }
    
    AriesScrollView *adView = [[AriesScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, ADImageView_MaxHeight) animationDuration:2.0f pageCount:(int)[ads count]];
    adView.centerY = self.view.height * 0.5;
    adView.backgroundColor = [UIColor grayColor];
    adView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.view addSubview:adView];
    
    adView.totalPagesCount = ^NSInteger(void){
        return imageViews.count;
    };
    adView.fetchContentViewAtIndex = ^(NSInteger index){
        
        return imageViews[index];
    };
}

#pragma mark - 点击方法
- (void)jumpToAdUrl:(UITapGestureRecognizer*)g {
    AdImgeView *imageView = (AdImgeView*)g.view;
    NSString *url = imageView.url;
    if (url) {
        if ([url isKindOfClass:[NSString class]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
